<template>
  <div
    class="scroll-container"
    @onmousewheel="handleScroll"
    ref="scrollContainer"
    @wheel.prevent="handleScroll"
  >
    <div class="scroll-wrapper" ref="scrollWrapper" :style="{left: left + 'px'}">
      <slot></slot>
    </div>
  </div>
</template>

<script>
const padding = 15 // tag's padding
import { mapGetters } from 'vuex'
export default {
  name: 'scrollPane',
  data() {
    return {
      left: 0,
      wrapperWidth: 0, //标签容器长度
      containerWidth: 0 //大容器宽
    }
  },
  computed: {
    ...mapGetters(['visitedViews']),
    visitedViews1() {
      return Object.assign([], this.visitedViews)
    }
  },
  watch: {
    // 监听当前打开的标签页
    visitedViews1(val, olVal) {
      // 关闭或打开标签页时重新获取标签容器宽度
      if (val.length !== olVal.length) {
        this.$nextTick(() => {
          this.wrapperWidth = this.$refs.scrollWrapper.offsetWidth
        })
      }
    },
    wrapperWidth(nVal, oVal) {
      let lableWidth = 0 //标签长度
      // 1.判断是否关闭一个标签
      if (nVal < oVal) {
        lableWidth = oVal - nVal
        // 2.判断标签页是否已经向左卷曲
        if (this.left < 0) {
          if (Math.abs(this.left) >= lableWidth) {
            this.left += lableWidth
          } else {
            this.left = 0
          }
        }
      }
    }
  },
  methods: {
    handleScroll(e) {
      const eventDelta = e.wheelDelta || -e.deltaY * 3
      const $container = this.$refs.scrollContainer
      const $containerWidth = $container.offsetWidth
      const $wrapper = this.$refs.scrollWrapper
      const $wrapperWidth = $wrapper.offsetWidth

      if (eventDelta > 0) {
        this.left = Math.min(0, this.left + eventDelta)
      } else {
        if ($containerWidth - padding < $wrapperWidth) {
          if (this.left < -($wrapperWidth - $containerWidth + padding)) {
            this.left = this.left
          } else {
            this.left = Math.max(
              this.left + eventDelta,
              $containerWidth - $wrapperWidth - padding
            )
          }
        } else {
          this.left = 0
        }
      }
    },
    moveToTarget($target) {
      const $container = this.$refs.scrollContainer
      const $containerWidth = $container.offsetWidth
      const $targetLeft = $target.offsetLeft
      const $targetWidth = $target.offsetWidth

      if ($targetLeft < -this.left) {
        // tag in the left
        this.left = -$targetLeft + padding
      } else if (
        $targetLeft + padding > -this.left &&
        $targetLeft + $targetWidth < -this.left + $containerWidth - padding
      ) {
        // tag in the current view
        // eslint-disable-line
      } else {
        // tag in the right
        this.left = -($targetLeft - ($containerWidth - $targetWidth) + padding)
      }
    }
  },
  mounted() {
    let $container = this.$refs.scrollContainer
    this.containerWidth = $container.offsetWidth
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.scroll-container {
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  width: 100%;
  .scroll-wrapper {
    position: absolute;
  }
}
</style>
